/*
 * GoogleWebHistoryXMLReader.java
 *
 * Created on April 23, 2007, 3:07 PM
 *
 *  Life Logger v1.0
    Copyright (C) 2007  Anand Kishore (anand@semanticvoid.com)
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.semanticvoid.lifelogger.xml.parser;

import com.semanticvoid.lifelogger.entity.BrowseHistoryEntity;
import com.semanticvoid.lifelogger.entity.SearchHistoryEntity;
import com.semanticvoid.lifelogger.xml.parser.interfaces.IReturnsBrowseObjects;
import com.semanticvoid.lifelogger.xml.parser.interfaces.IReturnsWebQueryObjects;
import com.semanticvoid.lifelogger.utils.DOMHelper;
import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This is a subclass of the XMLReader which is used for parsing the Google Web
 * History xml file. It implements the following interfaces:
 * * IReturnsBrowseObjects
 * * IReturnsWebQueryObjects
 * Hence it can be used to return both BrowseHistoryEntity and SearchHistoryEntity
 * objects.
 * @author anand kishore
 */
public class GoogleWebHistoryXMLReader extends XMLReader implements IReturnsBrowseObjects, IReturnsWebQueryObjects {
    
    /**
     * Creates a new instance of GoogleWebHistoryXMLReader
     * @param path the path of the Google Web History xml file
     */
    public GoogleWebHistoryXMLReader(String path) {
        super(path);
    }
    
    /**
     * Get the vector of the BrowseHistoryEntity objects from the xml document
     * @return the vector of BrowseHistoryEntity objects
     */
    public Vector<BrowseHistoryEntity> getBrowseResultObjects() {
        Vector<BrowseHistoryEntity> entities = new Vector<BrowseHistoryEntity>();
        
        for(String path : xmlFilePaths) {
            Document xmlDoc = formDocument(path);
            
            if(xmlDoc != null) {
                NodeList nodeList = xmlDoc.getElementsByTagName("item");
                
                if(nodeList != null) {
                    for(int index=0; index<nodeList.getLength(); index++) {
                        Node item = nodeList.item(index);
                        
                        if(DOMHelper.isBrowseEntity(item)) {
                            BrowseHistoryEntity be = DOMHelper.formBrowseEntity(item);
                            
                            if(be != null) {
                                entities.add(be);
                            }
                        }
                    }
                }
            }
        }
        
        return entities;
    }
    
    /**
     * Get the vector of the SearchHistoryEntity objects from the xml document
     * @return the vector of SearchHistoryEntity objects
     */
    public Vector<SearchHistoryEntity> getWebQueryObjects() {
        Vector<SearchHistoryEntity> entities = new Vector<SearchHistoryEntity>();
        
        for(String path : xmlFilePaths) {
            Document xmlDoc = formDocument(path);
            
            if(xmlDoc != null) {
                NodeList nodeList = xmlDoc.getElementsByTagName("item");
                
                if(nodeList != null) {
                    for(int index=0; index<nodeList.getLength(); index++) {
                        Node item = nodeList.item(index);
                        
                        if(DOMHelper.isSearchEntity(item)) {
                            SearchHistoryEntity se = DOMHelper.formSearchEntity(item);
                            
                            if(se != null) {
                                entities.add(se);
                            }
                        }
                    }
                }
            }
        }
        
        return entities;
    }
}
